package com.black.platform.role.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.common.web.PageQuery;
import com.black.platform.role.po.Role;
import com.black.platform.role.service.handler.RoleHandlerService;
import com.black.platform.role.vo.RolePermissionVo;
import com.black.platform.role.vo.RoleTreeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleController {

    private final RoleHandlerService roleHandlerService;

    @PostMapping("search")
    public ApiResponse<IPage<Role>> search(@RequestBody PageQuery<Role> searchVo) {
        return ApiResponseBuilder.ok(roleHandlerService.search(searchVo));
    }

    @GetMapping("permission/{id}")
    public ApiResponse<RolePermissionVo> getRolePermissionById(@PathVariable("id") Long id) {
        return ApiResponseBuilder.ok(roleHandlerService.getRolePermissionVoByRoleId(id));
    }

    @PostMapping("save")
    public ApiResponse<Boolean> save(@RequestBody RolePermissionVo rolePermissionVo) {
        return ApiResponseBuilder.ok(roleHandlerService.saveOrUpdate(rolePermissionVo));
    }

    @PostMapping("update")
    public ApiResponse<Boolean> update(@RequestBody RolePermissionVo rolePermissionVo) {
        return ApiResponseBuilder.ok(roleHandlerService.saveOrUpdate(rolePermissionVo));
    }

    @GetMapping("getRoleTree")
    public ApiResponse<List<RoleTreeVo>> getRoleTree(@RequestParam(required = false, value = "systemCode") String systemCode) {
        return ApiResponseBuilder.ok(roleHandlerService.getAllRoleTree(systemCode));
    }
}
